using System.Reflection;

namespace SiliverlightJavascriptIntelli
{
    public class SilverlightProperty
    {
        private string name;

        public SilverlightProperty(SilverlightType belongsTo)
        {
            this.belongsTo = belongsTo;
        }

        private string type;
        private bool hasSetter;
        private bool hasGetter;
        private bool isTypeInteger;
        private SilverlightType belongsTo;
        private string description;

        public SilverlightProperty(SilverlightType belongsTo, string Name, string Type, string Description, bool HasGetter, bool HasSetter)
        {
            this.BelongsTo = belongsTo;
            this.Name = Name;
            this.Type = Type;
            this.IsTypeInteger = TypeHelper.IsTypeInteger(Type);
            this.Description = Description;
            this.HasGetter = HasGetter;
            this.HasSetter = HasSetter;
        }

        public string Name
        {
            get
            {
                return StringHelper.FirstLetterToLower(name);
            }
            set
            {
                name = value;
            }
        }

        public string Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }

        public bool HasSetter
        {
            get
            {
                return hasSetter;
            }
            set
            {
                hasSetter = value;
            }
        }

        public bool HasGetter
        {
            get
            {
                return hasGetter;
            }
            set
            {
                hasGetter = value;
            }
        }

        public bool IsTypeInteger
        {
            get
            {
                return isTypeInteger;
            }
            set
            {
                isTypeInteger = value;
            }
        }

        public static SilverlightProperty ConvertPropertyInfoToSilverlightProperty(PropertyInfo curPropertyInfoToConvert, SilverlightType typeToAddTo)
        {
            SilverlightProperty newProp = new SilverlightProperty(typeToAddTo);

            newProp.Name = curPropertyInfoToConvert.Name;
            newProp.Type = TypeHelper.ConvertTypeToString(curPropertyInfoToConvert.PropertyType);
            newProp.IsTypeInteger = TypeHelper.IsTypeInteger(curPropertyInfoToConvert.PropertyType);
            newProp.HasGetter = curPropertyInfoToConvert.GetGetMethod() != null;
            newProp.HasSetter = curPropertyInfoToConvert.GetSetMethod() != null;
            newProp.Description = ReferenceAnalyzer.GetDescrptionForProperty(newProp.BelongsTo.Name, newProp.Name);

            return newProp;
        }

        public string GetGetterForProperty()
        {
            string format = GetJavascriptFormatForIndexerOrNormalProperty();

            return StringHelper.Format(format, this.BelongsTo.Name, this.Name, this.Type, this.IsTypeInteger.ToString(),
                 TypeHelper.GetConvertToPrefixForType(this.Type), this.Description);
        }



        private string GetJavascriptFormatForIndexerOrNormalProperty()
        {
            string format = string.Empty;
            if (!IsIndexer)
                format = JavascriptFormats.JavascriptGetter;
            else
                format = JavascriptFormats.JavascriptIndexerGetter;
            return format;
        }

        private bool IsIndexer
        {
            get
            {
                return Name.ToLower() == "item";
            }
        }

        public SilverlightType BelongsTo
        {
            get
            {
                return belongsTo;
            }
            set
            {
                belongsTo = value;
            }
        }

        private string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public string GetSetterForProperty()
        {
            if (!IsIndexer) 
                return StringHelper.Format(JavascriptFormats.JavascriptSetter, this.BelongsTo.Name, this.Name, this.Type, this.IsTypeInteger.ToString(),
                                      TypeHelper.GetExtractFromPrefixFor(this.Type), this.Description);

            return null;

        }

        //private string GetSetterFormatBasedOnIndexer()
        //{
        //    string format = string.Empty;
        //    if (!IsIndexer)
        //        format = JavascriptFormats.JavascriptSetter;
        //    else
        //        format = JavascriptFormats.JavascriptIndexerSetter;
        //    return format;
        //}

        public string ToJavascript()
        {
            string ReturnValue = string.Empty;

            if (this.HasSetter)
                ReturnValue += this.GetSetterForProperty();

            if (this.HasGetter)
                ReturnValue += this.GetGetterForProperty();

            return ReturnValue;
        }

        public override bool Equals(object obj)
        {
            if (obj is SilverlightProperty)
                return this.Name == ((SilverlightProperty)obj).Name
                        && this.Type == ((SilverlightProperty)obj).Type;
            else
                return base.Equals(obj);
        }

        public static bool operator ==(SilverlightProperty x, SilverlightProperty y)
        {
            bool isEqual;
            if (object.ReferenceEquals(x, null))
            {
                if (object.ReferenceEquals(y, null))
                {
                    isEqual = true;
                }
                else
                {
                    isEqual = false;
                }
            }
            else
            {
                isEqual = x.Equals(y);
            }
            return isEqual;
        }

        public static bool operator !=(SilverlightProperty x, SilverlightProperty y)
        {
            return !(x == y);
        }
    }
}